{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Implicit_solution.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "authorship_tag": "ABX9TyN4GKnPD0JRPf06eb49THZ5",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/Divyanshu-ISM/Oil-and-Gas-data-analysis/blob/master/Implicit_solution.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "xjtuC4gpEqm1",
        "colab_type": "text"
      },
      "source": [
        "#1D Reservoir Simulation - Implicit Method.\n",
        "\n",
        "Divyanshu Vyas | dvyas13ad@gmail.com\n",
        "\n",
        "Courtsey :- T. Ertekin, Abou Kassem et al.\n",
        "\n",
        "##Problem Statement:-\n",
        "\n",
        ">1D Reservoir, where unsteady state single phase oil flow is taking place. \n",
        "\n",
        ">Homogeneous Reservoir : phi = 30% | Kx = 178 mD\n",
        "\n",
        ">BC : No Flow Boundaries each.\n",
        "\n",
        ">Well Blocks:- 2 & 6 (Natural order) :- 2000 STB/D (Production).\n",
        "\n",
        ">Initial P (each GB) :- 4000 psia\n",
        "\n",
        ">Use delta T = 10 days. \n",
        "\n",
        ">Other props: mu = 1 cP | B = 1 RB/STB | c = 5*10^-6 psi-1\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Evc_hAA0Ggc3",
        "colab_type": "text"
      },
      "source": [
        "![7.1.JPG]()"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vp1ioA9PKn1V",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "import matplotlib.pyplot as plt\n",
        "%matplotlib inline"
      ],
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "iHtAhLgREbZw",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "qsc = np.array([0,-2000,0,0,0,-2000,0]).reshape(7,1)\n",
        "\n",
        "phi = 0.30\n",
        "\n",
        "kx = 178\n",
        "\n",
        "P_i = np.ones(7)*4000 #psia\n",
        "\n",
        "dt = 10 #days\n",
        "\n",
        "mu = 1\n",
        "\n",
        "B = 1\n",
        "\n",
        "c = 5E-6\n",
        "\n",
        "\n",
        "dx = 400\n",
        "dy = 200\n",
        "h = 100\n",
        "# x = np.arange(dx/2,L - dx/2 , dx)\n",
        "\n",
        "t = np.arange(0,370,dt)\n",
        "\n",
        "p = np.zeros((len(t),len(P_i)))\n",
        "\n",
        "P = pd.DataFrame(p)\n",
        "\n"
      ],
      "execution_count": 78,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "RA7cPBELNpop",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 197
        },
        "outputId": "a455558e-5338-4696-ac10-03dc94bf6b06"
      },
      "source": [
        "P.loc[0,:] = P_i\n",
        "P.head()"
      ],
      "execution_count": 79,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>0</th>\n",
              "      <th>1</th>\n",
              "      <th>2</th>\n",
              "      <th>3</th>\n",
              "      <th>4</th>\n",
              "      <th>5</th>\n",
              "      <th>6</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>4000.0</td>\n",
              "      <td>4000.0</td>\n",
              "      <td>4000.0</td>\n",
              "      <td>4000.0</td>\n",
              "      <td>4000.0</td>\n",
              "      <td>4000.0</td>\n",
              "      <td>4000.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "        0       1       2       3       4       5       6\n",
              "0  4000.0  4000.0  4000.0  4000.0  4000.0  4000.0  4000.0\n",
              "1     0.0     0.0     0.0     0.0     0.0     0.0     0.0\n",
              "2     0.0     0.0     0.0     0.0     0.0     0.0     0.0\n",
              "3     0.0     0.0     0.0     0.0     0.0     0.0     0.0\n",
              "4     0.0     0.0     0.0     0.0     0.0     0.0     0.0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 79
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3Uc2J1ZEoVft",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "36276b41-e097-455a-e0d7-41f7430849d7"
      },
      "source": [
        ""
      ],
      "execution_count": 47,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(7, 1)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 47
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "M21KiMNKN66s",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#Initial Calculations\n",
        "\n",
        "#M = mu*B*dx/(beta*kx*Ax)\n",
        "Ax = dy*h\n",
        "\n",
        "\n",
        "M = (mu*B*dx)/(0.001127*kx*Ax)\n",
        "\n",
        "R = (phi*mu*c*dx**2)/(0.001127*5.615*kx*dt)"
      ],
      "execution_count": 80,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QKssb2emOTcE",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "014b787f-6080-4c49-e12a-0103660b0b14"
      },
      "source": [
        "# M = 0.1\n",
        "# R = 0.021"
      ],
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0.021306767298291878"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 21
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ggNEfAHaRuUn",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#Matrix looks like this:-\n",
        "\n",
        "# -3-R    1       0     0     0     0     0\n",
        "#   1   (-2-R)    1     0     0     0     0\n",
        "#   0      1   (-2-R)   1     0     0     0\n",
        "#   0      0      1   (-2-R)  1     0     0\n",
        "#   0      0      0     1   (-2-R)  1     0\n",
        "#   0      0      0     0     1   (-2-R)  1\n",
        "#   0      0      0     0     0     1   (-3-R)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wPX-JrcUVcWv",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# np.diag?"
      ],
      "execution_count": 22,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "byDp8u_EUp9t",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# T = np.zeros((7,7))\n",
        "\n",
        "\n",
        "# d_0 = np.array([(-3-R),(-2-R),(-2-R),(-2-R),(-2-R),(-2-R),(-3-R)])\n",
        "\n",
        "# d_1 = np.array([1,1,1,1,1,1])\n",
        "\n",
        "\n",
        "# # np.diag(T,k=0) = d_0.values()\n",
        "\n",
        "# # np.diag(T,k=1) = d_1\n",
        "\n",
        "# # np.diag(T,k=-1) = d_1\n",
        "\n",
        "# np.fill_diagonal(T,d_0)"
      ],
      "execution_count": 33,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zl8egkeUV8fS",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# # print(T)\n",
        "# tri_dia = np.tri(7,7,k=0,dtype='float') this is for a triangular matrix."
      ],
      "execution_count": 38,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VOc3He4Ke2uZ",
        "colab_type": "text"
      },
      "source": [
        "Important Step : Creating the Sparse Matrix"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vocOrDUDYCWb",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from scipy.sparse import diags\n",
        "diagonals = [d_0,d_1,d_1]\n",
        "\n",
        "T = diags(diagonals, [0, 1, -1]).toarray()"
      ],
      "execution_count": 81,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "CncGQIN2o2tK",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#"
      ],
      "execution_count": 51,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8vhhjrw4pdsn",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "4248db8d-cdf8-464f-fe63-536406ea937a"
      },
      "source": [
        ""
      ],
      "execution_count": 53,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(7,)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 53
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "OtrEsN4Osb96",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from scipy.linalg import solve"
      ],
      "execution_count": 82,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fW_2BA7gd7u9",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# #Now RHS matrix\n",
        "# for j in range(1,len(t)):\n",
        "#   Pi_n = np.array(P.loc[j-1,:]).reshape(7,1)\n",
        "\n",
        "#   rhs = -M*qsc -R*(np.array(P.loc[j-1,:]).reshape(7,1))\n",
        "\n",
        "#   rhs = rhs.reshape(7,1)\n",
        "\n",
        "#   x = solve(T,rhs)\n",
        "\n",
        "#   P.loc[j,:] = x.reshape(7)\n",
        "\n",
        "p1 = np.array(P.loc[0,:]).reshape(7)\n",
        "\n",
        "# rhs = -M*qsc - R*p1"
      ],
      "execution_count": 92,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ht9VCTe8ezE0",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 52
        },
        "outputId": "a7ae2a2b-04ff-4ce1-fc4c-4446c270079c"
      },
      "source": [
        "rhs = -M*qsc - R*p1\n",
        "\n",
        "x = solve(T,rhs)\n",
        "\n",
        "x"
      ],
      "execution_count": 98,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([ 45.418662  ,  51.99664168, 173.85126313, 214.18302381,\n",
              "       173.85126313,  51.99664168,  45.418662  ])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 98
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8KCbeNxYquIq",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 52
        },
        "outputId": "c879eb72-db79-493c-c3bd-a09c92e4bd7d"
      },
      "source": [
        "p1 = x\n",
        "\n",
        "rhs = -M*qsc - R*p1\n",
        "\n",
        "x = solve(T,rhs)\n",
        "\n",
        "x\n"
      ],
      "execution_count": 99,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([ -87.88328775, -266.49009686, -252.48699818, -247.56778961,\n",
              "       -252.48699818, -266.49009686,  -87.88328775])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 99
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "lJNkYxT3r8GU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}